import fs from 'fs';
import path from 'path';
const __dirname = path.resolve();
const copyFolder = (copiedPath, resultPath, direct) => {
  if (!direct) {
    copiedPath = path.join(__dirname, copiedPath);
    resultPath = path.join(__dirname, resultPath);
  }
  if (fs.existsSync(copiedPath)) {
    const files = fs.readdirSync(copiedPath, { withFileTypes: true });
    for (let i = 0; i < files.length; i++) {
      const cf = files[i];
      if (cf.name !== 'custom.txt') {
        const ccp = path.join(copiedPath, cf.name);
        const crp = path.join(resultPath, cf.name);
        if (cf.isFile()) {
          const readStream = fs.createReadStream(ccp);
          const writeStream = fs.createWriteStream(crp);
          readStream.pipe(writeStream);
          console.log(`form ${ccp} copy to ${crp}`);
        } else {
          try {
            fs.accessSync(crp, fs.constants.F_OK);
          } catch (error) {
            fs.mkdirSync(crp);
          } finally {
            copyFolder(ccp, crp, true);
          }
        }
      }
    }
  } else {
    console.log('do not exist path: ', copiedPath);
  }
};
console.log(`start copy custom code...`);
copyFolder('user', 'src', false);
console.log(`complete copy custom code...`);
